package com.nit.lab.vote.controller.vote;

import com.nit.lab.vote.dto.system.TokenDTO;
import com.nit.lab.vote.dto.utils.OpResultDTO;
import com.nit.lab.vote.dto.utils.PasswordDTO;
import com.nit.lab.vote.dto.utils.TableReqDTO;
import com.nit.lab.vote.dto.utils.TableRspDTO;
import com.nit.lab.vote.dto.vote.CPDTO;
import com.nit.lab.vote.dto.vote.ProjectDTO;
import com.nit.lab.vote.dto.vote.UPDTO;
import com.nit.lab.vote.service.vote.ProjectService;
import com.nit.lab.vote.utils.ExportUtil;
import com.nit.lab.vote.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/project")
public class ProjectController {
    @Resource
    private ProjectService projectService;
    private final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @RequestMapping(value = "/addProject" , method = RequestMethod.POST)
    public OpResultDTO addProject(@RequestHeader(name = "Access-Token") String token,@RequestBody ProjectDTO projectDTO){
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO != null) {
                if (tokenDTO.getRoleId() == 1 || tokenDTO.getRoleId() == 0) {
                    op.setMessage(projectService.addProject(tokenDTO.getUserName(),projectDTO).toString());
                } else {
                    op.setMessage("error1");
                }
            } else {
                op.setMessage("error2");
            }
        } catch (Exception e) {
            op.setMessage("error3");
        }
        return op;
    }

    @RequestMapping(value = "/updateProject", method = RequestMethod.POST)
    public OpResultDTO updateProject(@RequestBody ProjectDTO projectDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setMessage("success");
            op.setResult(projectService.updateProject(projectDTO));
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }
    @RequestMapping(value = "/deleteProject" , method = RequestMethod.POST)
    public OpResultDTO deleteProject(@RequestBody ProjectDTO projectDTO){
        OpResultDTO op = new OpResultDTO();
        try {
            op.setMessage("success");
            op.setResult(projectService.deleteProject(projectDTO));
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    public TableRspDTO listAll(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = projectService.listAll(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return  tableRspDTO;
    }

    @RequestMapping(value = "/listUserProject", method = RequestMethod.POST)
    public TableRspDTO listUserProject(@RequestHeader(name = "Access-Token") String token,  @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        TokenDTO tokenDTO = JWTUtil.verifyToken(token);
        try {
            tableRspDTO = projectService.listUserProject(tokenDTO.getUserId(), tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return  tableRspDTO;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody ProjectDTO projectDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            op.setMessage("success");
            op.setResult(projectService.update(projectDTO));
        } catch (Exception e) {
            logger.error(e.toString());
            op.setMessage("error");
        }
        return op;
    }

    /**
     * @Description: 根据projectId查询评委
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/listByProject", method = RequestMethod.POST)
    public TableRspDTO listByProject( @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = projectService.listByProject(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 编辑候选人
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/queryAllProject", method = RequestMethod.POST)
    public TableRspDTO queryAllProject( @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = projectService.queryAllProject(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }


    /**
     * @Description: 为项目删除评委
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/removeByProject",method = RequestMethod.POST)
    public OpResultDTO removeUserProject(@RequestBody UPDTO upDTO) {
        OpResultDTO opResultDTO = new OpResultDTO();
        try {
            opResultDTO.setMessage("success");
            opResultDTO.setResult(projectService.removeUserProject(upDTO));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return opResultDTO;
    }

    /**
     * @Description: 为项目添加评委
     * @Date: 2020/12/12
     */
    @RequestMapping(value = "/addByProject", method = RequestMethod.POST)
    public OpResultDTO addByProject(@RequestBody UPDTO upDTO) {
        OpResultDTO op;
        try {
            op = projectService.addByProject(upDTO);
        } catch (Exception e) {
            op = new OpResultDTO();
            op.setMessage("error");
            op.setResult("添加失败");
        }
        return op;
    }
    @RequestMapping(value = "/download", method = RequestMethod.POST)
    public void exportExcel(HttpServletResponse response ,@RequestBody TableReqDTO tableReqDTO) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filename = "demo.xls";
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        List<List<String>> dataList = new ArrayList<>();
        List<PasswordDTO> passwordDTOS = projectService.listAllProject4Download(tableReqDTO);
//        ArrayList<String> list;
//        for (int i = 0; i < 100; i++) {
//            list = new ArrayList<>();
//            for (int j = 0; j < 10; j++) {
//                list.add("i=" + i + "\t" + j);
//            }
//            dataList.add(list);
//        }
//        List<List<String>> dataList = new ArrayList<>();
        ArrayList<String> list;
        for (int i = 0; i < passwordDTOS.size(); i++) {
            list = new ArrayList<>();
            list.add(passwordDTOS.get(i).getKey().toString());
            list.add(passwordDTOS.get(i).getName());
            list.add(passwordDTOS.get(i).getPhone().toString());
            list.add(passwordDTOS.get(i).getPassword());
            dataList.add(list);
        }
        List<String> headList = new ArrayList<>();
        headList.add("序号");
        headList.add("姓名");
        headList.add("手机号");
        headList.add("密码");
        byte[] bytes = ExportUtil.exportExcel(headList, dataList, "xls");

        response.getOutputStream().write(bytes);
    }
}
